package com.rabbiter.oes.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbiter.oes.entity.Major;
import com.rabbiter.oes.mapper.ExamManageMapper;
import com.rabbiter.oes.mapper.MajorMapper;
import com.rabbiter.oes.mapper.StudentMapper;
import com.rabbiter.oes.mapper.TeacherMapper;
import com.rabbiter.oes.service.MajorService;
import com.rabbiter.oes.vo.MajorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements MajorService {

    // 学生
    @Autowired
    private StudentMapper studentMapper;

    // 试卷
    @Autowired
    private ExamManageMapper examManageMapper;

    @Override
    public IPage<MajorVO> findPage(Page<MajorVO> page, String name) {
        name = ("@".equals(name) ? "" : name);
        return baseMapper.findPage(page, name);
    }

    @Override
    public List<Major> findAll() {
        return baseMapper.selectList(Wrappers.lambdaQuery());
    }

    @Override
    public Long findCountByName(String name) {
        return baseMapper.findCountByName(name);
    }

    @Override
    public Major findById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public int deleteById(Integer id) {
        Assert.isTrue(ObjectUtil.isNotEmpty(id), "学院id不能为空");
        Long studentCount = studentMapper.selectCountByCollegeId(id);
        Assert.isTrue((studentCount == 0), "该专业有学生，不允许删除操作。");

        Long examCount = examManageMapper.selectCountByCollegeId(id);
        Assert.isTrue((examCount == 0), "该专业有试卷，不允许删除操作。");

        return baseMapper.deleteById(id);
    }

    @Override
    public int update(Major major) {
        return baseMapper.updateById(major);
    }

    @Override
    public int add(Major major) {
        return baseMapper.insert(major);
    }
}
